package com.example.dryclean_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.News;
import com.example.dryclean_admin.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiaxin
 * @since 2023-12-30
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    INewsService iNewsService;

    @RequestMapping("/listPageR")
    public Result listPageR(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String title = (String)param.get("title");
        Page<News> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(title) && !"null".equals(title)){
            lambdaQueryWrapper.like(News::getTitle,title);
        }
        IPage result = iNewsService.page(page,lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());
        return Result.success(result.getRecords(), result.getTotal());
    }

    @PostMapping("/save")
    public Result save(@RequestBody News news){
        return iNewsService.save(news)?Result.success():Result.fail();
    }



    @PostMapping("/update")
    public Result update(@RequestBody News news){
        return iNewsService.updateById(news)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return iNewsService.removeById(id) ? Result.success() : Result.fail();
    }
}
